/**
 * Created by xiao_XX on 2017/9/6.
 * 单选树
 */
import React from 'react';
import { Link } from 'dva/router';
import { connect } from 'dva';
import PropTypes from 'prop-types';
import { TreeSelect } from 'antd';
const TreeNode = TreeSelect.TreeNode;
//const SHOW_CHILD = TreeSelect.SHOW_CHILD;


const SmartTreeSelectInput = ({treeDate, values, dispatch, keyItem, type, routeItem}) => {

  //const {type,values} = sigleTreeSelect;


  /**
   * 当选中的时候  修改显示值
   * @param value
   */
  function onChange (value){
    var t = {};
    t[keyItem] = value;
    dispatch({
      type: type + '/changeState',
      key: keyItem,
      value: value
    });
    dispatch({
      type: type + '/singleTreeCallBack',
      payload: value,
      key: keyItem
    });
    if (routeItem != undefined && routeItem != ""){
      dispatch({
        type: type + routeItem,
        payload:{
          value:value
        }
      });
    }
    // props.onChange(value);
  }


  /**
   * 循环输出数的结构
   * @type {Array}
   */
  var treeOneArr = [];

  for(var i =0; i<treeDate.length;i++) {

    if(treeDate[i].two!=null){

      var treeTowArr = [];
      for (var j = 0; j < treeDate[i].two.length; j++) {

        var treeThreeArr = [];
        if(treeDate[i].two[j].three!=null){
          for (var k = 0; k < treeDate[i].two[j].three.length; k++) {
            treeThreeArr.push(
              <TreeNode title={treeDate[i].two[j].three[k].title} value={treeDate[i].two[j].three[k].value} key={`${i}-${j}-${k}`}/>
            )
          }
        }
        treeTowArr.push(
          <TreeNode title={treeDate[i].two[j].title} value = {treeDate[i].two[j].value} key={`${i}-${j}`}>
            {treeThreeArr}
          </TreeNode>
        )
      }
    }

    treeOneArr.push(
      <TreeNode title={treeDate[i].title} value = {treeDate[i].value} key={`${i}-0`}>
        {treeTowArr}
      </TreeNode>
    )
  }



  return(
    <TreeSelect
      showSearch
      style={{ width: '100%', height: 30 }}
      value={values}
      dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
      placeholder="请选择"
      allowClear
      treeDefaultExpandAll
      onChange={onChange}
      treeNodeFilterProp='title'
      size="default"
    >
      {treeOneArr}
    </TreeSelect>
  )

};

//SmartTreeSelectInput.propTypes = {
//  onChange: PropTypes.func.isRequired,
//};

export default connect(({}) => ({}))(SmartTreeSelectInput);

